package com.hang.gymmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hang.gymmanager.Enum.CommonConstant;
import com.hang.gymmanager.Enum.PlaceTypeEnum;
import com.hang.gymmanager.Enum.PlaceUseRecordTypeEnum;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.convert.PojoConvert;
import com.hang.gymmanager.entity.*;
import com.hang.gymmanager.exception.BusinessException;
import com.hang.gymmanager.mapper.CourseMapper;
import com.hang.gymmanager.mapper.SeatMapper;
import com.hang.gymmanager.res.CenterCoursePageRes;
import com.hang.gymmanager.res.CenterOrderPageRes;
import com.hang.gymmanager.res.CenterPlacePageRes;
import com.hang.gymmanager.res.CenterTicketPageRes;
import com.hang.gymmanager.service.*;
import com.hang.gymmanager.util.PasswordEncodeUtil;
import com.hang.gymmanager.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/3 19:55
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserCenterServiceImpl implements UserCenterService {

    private final OrderService orderService;
    private final PojoConvert pojoConvert;
    private final CourseMapper courseMapper;
    private final PlaceUseRecordService placeUseRecordService;
    private final SeatMapper seatMapper;

    private final UserInfoService userInfoService;

    @Override
    public Page<CenterOrderPageRes> order(BasePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        QueryWrapper<OrderEntity> wrapper = new QueryWrapper<>();
        Integer userId = SecurityUtils.getUserId();
        wrapper.lambda().eq(OrderEntity::getUserId,userId)
                .orderByDesc(OrderEntity::getFinishTime);
        Page result = orderService.getBaseMapper().selectPage(page, wrapper);
        List<OrderEntity> records = result.getRecords();
        List<CenterOrderPageRes> info = pojoConvert.UserCenterOrderEntity2Res(records);
        result.setRecords(info);
        return result;
    }

    @Override
    public Page<CenterCoursePageRes> course(BasePageReq req) {
        Integer userId = SecurityUtils.getUserId();
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<CenterCoursePageRes> result = courseMapper.userCenterCourse(userId, page);
        return result;
    }

    @Override
    public Page<CenterPlacePageRes> place(BasePageReq req) {
        Integer userId = SecurityUtils.getUserId();
        Page page = new Page(req.getPageNum(),req.getPageSize());
        QueryWrapper<PlaceUseRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PlaceUseRecordEntity::getType, PlaceUseRecordTypeEnum.PERSONAL.getValue())
                        .eq(PlaceUseRecordEntity::getRelationId,userId)
                        .orderByDesc(PlaceUseRecordEntity::getDay)
                        .orderByDesc(PlaceUseRecordEntity::getStartTime);
        Page info = placeUseRecordService.getBaseMapper().selectPage(page, wrapper);
        List<PlaceUseRecordEntity> list = info.getRecords();
        List<CenterPlacePageRes> result = pojoConvert.centerPlaceEntity2Res(list);
        info.setRecords(result);
        return info;
    }

    @Override
    public Page<CenterTicketPageRes> ticket(BasePageReq req) {
        Integer userId = SecurityUtils.getUserId();
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<CenterTicketPageRes> result = seatMapper.userCenterTicket(userId, page);
        return result;
    }

    @Override
    public void reset() {
        //获取用户id
        Integer userId = SecurityUtils.getUserId();
        String encode = PasswordEncodeUtil.passwordEncode(CommonConstant.NORMAL_PASSWORD,CommonConstant.PASSWORD_SALT);
        UserInfoEntity entity = new UserInfoEntity();
        entity.setUserId(userId);
        entity.setPassword(encode);
        int i = userInfoService.getBaseMapper().updateById(entity);
        if(i!=1){
            throw new BusinessException("重置用户密码失败，请重试");
        }
    }
}
